from PySide6.QtWidgets import QWidget
from ..ScriptWidget import ScriptWidget
from .VarCompare import VarCompare
from .TextContain import TextContain
from .PicContain import PicContain
from enums.ConditionType import ConditionType
from enums.ZhConditionType import ZhConditionType


class Condition(QWidget):
    def __init__(self, parent: ScriptWidget):
        super(Condition, self).__init__(parent)

        self.setFixedSize(parent.width(), parent.lineHeight * 2 + parent.intervalY)

        self.varCompare = VarCompare(self)
        self.textContain = TextContain(self)
        self.picContain = PicContain(self)
        self.conditionType = ConditionType.VAR_COMPARE

    @property
    def conditionType(self):
        return self._conditionType

    @conditionType.setter
    def conditionType(self, conditionType):
        self._conditionType = conditionType
        self.varCompare.setHidden(conditionType != ConditionType.VAR_COMPARE)
        self.textContain.setHidden(conditionType != ConditionType.TEXT_CONTAIN)
        self.picContain.setHidden(conditionType != ConditionType.PIC_CONTAIN)

    @property
    def zhConditionType(self):
        return ZhConditionType[self.conditionType.name]

    @zhConditionType.setter
    def zhConditionType(self, zhConditionType):
        self.conditionType = ConditionType[zhConditionType.name]

    @property
    def data(self):
        result = {
            "condition_type": self.conditionType.value,
        }

        if self.conditionType == ConditionType.VAR_COMPARE:
            result['condition_info'] = self.varCompare.data
        elif self.conditionType == ConditionType.TEXT_CONTAIN:
            result['condition_info'] = self.textContain.data
        elif self.conditionType == ConditionType.PIC_CONTAIN:
            result['condition_info'] = self.picContain.data

        return result

    @data.setter
    def data(self, data):
        conditionInfo = data['condition_info']
        if self.conditionType == ConditionType.VAR_COMPARE:
            self.varCompare.data = conditionInfo
        elif self.conditionType == ConditionType.TEXT_CONTAIN:
            self.textContain.data = conditionInfo
        elif self.conditionType == ConditionType.PIC_CONTAIN:
            self.picContain.data = conditionInfo
